<template>
  <!-- 4.1.3.1租户信息管理 -->
  <div class="TenantInformationManagement">
    <div style="padding: 0px 0px;">
      <!-- 搜索 -->
      <!-- <SearchCForL
        :searchData="dataModel"
        @searchCallBack="searchCallBack"
      ></SearchCForL> -->
      <searchComponentsCss :key="searchKey" :searchData="dataModel" @searchCallBack="searchCallBack" :optionsZi="optionsZi" :searchOptionTable="searchOptionTable" :shrinkBoxOpenClose='shrinkBoxOpenClose'></searchComponentsCss>
      <!-- 收缩框 -->
      <!-- {{numSearch}} -->
      <div class="shrinkBox" @click="shrinkBoxOpenClose = !shrinkBoxOpenClose">
        <i :class="shrinkBoxOpenClose?'el-icon-arrow-up':'el-icon-arrow-down'" v-if="numSearch>8"></i>
      </div>
      <!-- @clearSearch='clearSearch' -->
      <div style="border-top:1px solid #E6E6E6;margin:0 30px;"></div>
      <div style="padding: 25px 30px;">
        <!--添加和批量删除组件-->
        <addComponent class="buttonhurdle" :diaDataAdd="dataModel" :optionsZi="optionsZi" :searchOptionTable="searchOptionTable" @toAdd="toAdd"></addComponent>
        <!-- <buttonhurdle
          class="buttonhurdle"
          :diaDataAdd="dataModel"
          :optionsZi="optionsZi"
          :searchOptionTable="searchOptionTable"
          @toAdd="toAdd"
          @toDelete="toDelete"
        ></buttonhurdle> -->
        <!-- 右侧公共组件 -->
        <rightButtons style="float:right;padding:10px;" @sunS_F='sunS_F' @configuration='configuration' class="right_buttons" @emptySearch='emptySearch'></rightButtons>
        <!-- <div class="buttons" style="float:right;padding:10px;">
          <el-button
            type="primary"
            size="mini"
            plain
            @click="sunS_F"
            icon="el-icon-search"
          >搜索</el-button>
          <el-button
            type="primary"
            size="mini"
            plain
            icon="el-icon-refresh-right"
            @click="emptySearch"
          >重置</el-button>
          <el-button
            type="primary"
            size="mini"
            plain
            icon="el-icon-notebook-2"
          >配置</el-button>
        </div> -->
      </div>

    </div>
    <div class="out" style="padding:0px 30px">
      <!-- 字典 -->
      <dictionaries :searchData='dataModel' :modelType='modelType' @optionData="dictionariesCallBack"></dictionaries>
      <!--数据列表-->
      <el-table ref="multipleTable" :data="dataAttribute" @selection-change="handleSelectionChange" v-loading.body="false" border fit max-height="511px" highlight-current-row size="mini" :cell-style="{padding:'5px 0'}">
        <el-table-column align="center" type="selection" :show-overflow-tooltip="true" width="45"></el-table-column>
        <el-table-column align="center" label="序号" type="index" :index="indexMethod" width="54px">
        </el-table-column>
        <el-table-column align="center" min-width="140px" v-for="item in dataModel" v-if="item.tableSign" :key="item.id" :label="item.CN" :show-overflow-tooltip="true">
          <!-- show-overflow-tooltip -->
          <!-- width="120px" -->
          <!-- <template slot-scope="scope">{{scope.row[item.EN]}}</template> -->
          <template slot-scope="scope">{{item.type === 'select' ? optionDatas(item.typeGroupCode?optionsZi[item.EN]:searchOptionTable[item.EN],scope.row[item.EN]):scope.row[item.EN]}}</template>
        </el-table-column>
        <el-table-column width="180px" align="center" label="操作">
          <template slot-scope="scope">
            <!-- 查看按钮 -->
            <el-button style="color:#0B9AC3;border:none;" icon="el-icon-view" circle @click="makeExaId(scope.row)"></el-button>
            <!-- 修改组件 -->
            <!-- {{scope.row}} -->
            <modificationi class="inline_block" :attributeOrder="dataModel" :modData="scope.row" @modifyData="modifyData" :optionsZi="optionsZi" :searchOptionTable="searchOptionTable"></modificationi>
            <!-- 删除按钮 -->
            <el-button style="color:#C90010;border:none;" icon="el-icon-delete" circle @click="deleteList(scope.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination style="margin-top:15px;" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="pagesize" layout="total, sizes, prev, pager, next, jumper" :total="pagetotal"></el-pagination>
    </div>
    <!-- 查看组件 -->
    <examine :DialogVisibleMod="DialogVisibleExamine" :modData="examineData" :attributeOrder="dataModel" @examineDialog="examineDialog" :optionsZi="optionsZi" :searchOptionTable="searchOptionTable"></examine>
  </div>
</template>
<script>
//引入组件
import {
  SearchCForL,
  buttonhurdle,
  // modificationi,
  examine,
  searchComponents,
  searchComponentsCss,
  dictionaries,
  // addComponent,
  rightButtons,
} from '@/views/components/FLPage'
import addComponent from './compoetnts/addComponent'
import modificationi from './compoetnts/modificationi'
//引入api ajax
import {
  postbaseTenantInformation, //增加
  deleteBaseTenantInformation, //单项删除
  searchPageSize, //分页查询
  baseTenantInformation, //修改
  examineBaseTenantInformation, //查看
  //根据ID获取
} from '@/api/TenantInformationManagement'
export default {
  components: {
    buttonhurdle,
    modificationi,
    examine,
    searchComponents,
    searchComponentsCss,
    SearchCForL,
    dictionaries,
    addComponent,
    rightButtons,
  },
  name: 'TenantInformationManagement',
  data () {
    return {
      numSearch: 0,
      //   收缩框开关
      shrinkBoxOpenClose: false,
      // 搜索框刷新组件
      searchKey: 0,
      // indexMethods:'',
      menuTrigger: '',
      //搜索
      searchContent: {},
      //后端传来的数据
      dataAttribute: [],
      //分页
      currentPage: 1, //当前页
      pagesize: 10, //每页条数
      pagetotal: 0, //总条数
      //查看的数据
      examineData: {},
      //查看的弹窗控制器
      DialogVisibleExamine: false,
      //表格中选中的数组
      multipleSelection: [],
      // 字典请求字段数据
      modelType: 'table',
      optionsZi: [],
      searchOptionTable: [],
      //数据模型
      dataModel: [
        // {
        //   EN: "id",
        //   CN: "序号",
        //   value: "",
        //   type: "input",
        //   option: [],
        //   typeFrom: "string",
        //   Exp: "",
        //   err: "·长度为3-20的所有字符!",
        //   result: true,
        //   sign: true,
        //   searchSign: false,
        //   addSign: false,
        //   tableSign: true,
        //   examineSign: true,
        //   examineDisabled: false,
        //   modificationSign: false
        // },
        {
          EN: 'sysCompanyCode',
          CN: '租户编号',
          value: '',
          type: 'input',
          option: [],
          typeFrom: 'string',
          Exp: '^.{2,20}$',
          err: '·2-20的字符!',
          result: true,
          sign: true,
          searchSign: true,
          addSign: false,
          tableSign: true,
          examineSign: true,
          examineDisabled: false,
          modificationSign: false,
        },
        {
          EN: 'name',
          CN: '租户名称',
          value: '',
          type: 'input',
          option: [],
          typeFrom: 'string',
          // Exp: "^.{2,20}$",
          Exp: '^([\u4e00-\u9fa5]|[.])*$',
          err: '只能输入汉字！',
          result: true,
          sign: true,
          searchSign: true,
          addSign: true,
          tableSign: true,
          examineSign: true,
          examineDisabled: false,
          modificationSign: true,
        },
        {
          EN: 'type',
          CN: '租户类型',
          value: '',
          type: 'select',
          option: [
            {
              value: 1,
              label: '是',
            },
            {
              value: 0,
              label: '否',
            },
          ],
          typeFrom: 'string',
          Exp: '.{1,20}$',
          err: '·1-20的字符!',
          result: true,
          sign: true,
          searchSign: true,
          addSign: true,
          tableSign: true,
          examineSign: true,
          examineDisabled: false,
          modificationSign: true,
          typeGroupCode: 'we',
        },
        {
          EN: 'phone',
          CN: '手机号码',
          value: '',
          type: 'input',
          option: [],
          typeFrom: 'string',
          // Exp: '^1[0-9]{10}$',
          Exp: '^[0-9-]{0,30}$',
          err: '·长度为0-30位电话号!',
          result: true,
          sign: true,
          searchSign: false,
          addSign: true,
          tableSign: true,
          examineSign: true,
          examineDisabled: false,
          modificationSign: true,
        },

        {
          EN: 'location',
          CN: '办公地址',
          value: '',
          type: 'input',
          option: [],
          typeFrom: 'string',
          Exp: '.{1,20}$',
          err: '·长度为1-20的所有字符!',
          result: true,
          sign: true,
          searchSign: false,
          addSign: true,
          tableSign: true,
          examineSign: true,
          examineDisabled: false,
          modificationSign: true,
        },
        {
          EN: 'latitude',
          CN: '纬度',
          value: '',
          type: 'input',
          option: [],
          typeFrom: 'string',
          Exp: '',
          err: '·长度为1-20的所有字符!',
          result: true,
          sign: true,
          searchSign: false,
          addSign: true,
          tableSign: false,
          examineSign: true,
          examineDisabled: false,
          modificationSign: true,
        },
        {
          EN: 'longitude',
          CN: '经度',
          value: '',
          type: 'input',
          option: [],
          typeFrom: 'string',
          Exp: '.{1,20}$',
          err: '·长度为1-20的所有字符!',
          result: true,
          sign: true,
          searchSign: false,
          addSign: true,
          tableSign: false,
          examineSign: true,
          examineDisabled: false,
          modificationSign: true,
        },
        {
          EN: 'createDate',
          CN: '创建时间',
          value: '',
          type: 'date',
          option: [],
          typeFrom: 'string',
          Exp: '',
          err: '',
          result: true,
          sign: true,
          searchSign: false,
          addSign: false,
          tableSign: true,
          examineSign: false,
          examineDisabled: false,
          modificationSign: false,
        },
        {
          EN: 'notes',
          CN: '备注',
          value: '',
          type: 'input',
          option: [],
          typeFrom: 'string',
          Exp: '',
          err: '',
          result: true,
          sign: true,
          searchSign: false,
          addSign: true,
          tableSign: true,
          examineSign: true,
          examineDisabled: false,
          modificationSign: true,
          skipSign: true,
        },
      ],
    }
  },
  created () {
    this.forData(this.currentPage, this.pagesize, this.searchContent)
  },
  methods: {
    // 搜索框
    getSSearchNum () {
      let searchData = this.searchData
      let num = 0
      for (let i = 0; i < searchData.length; i++) {
        const element = searchData[i]
        if (element.searchSign) {
          num += 1
        } else {
          num = num
        }
      }
      this.numSearch = num
    },
    // 配置
    configuration (msg) {
      if (msg == '配置') {
        console.log(msg)
      }
    },
    // 重置
    emptySearch (msg) {
      if (msg == '重置') {
        let searchContent = this.searchContent
        console.log(this.searchContent, 855555)

        for (const key in this.searchContent) {
          if (this.searchContent.hasOwnProperty(key)) {
            this.searchContent[key] = ''
          }
        }
        console.log(this.dataModel, 966666)
        for (let i = 0; i < this.dataModel.length; i++) {
          this.dataModel[i].value = ''
        }
        console.log(this.searchContent, 'kankanjiguo')
        console.log(this.dataModel, 'dataModel')
        this.searchKey = this.searchKey + 1

        // this.forData(this.currentPage, this.pagesize);
        this.currentPage = 1
        this.forData(this.currentPage, this.pagesize, this.searchContent)
      }
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
      console.log(this.multipleSelection)
    },
    //查看关闭弹窗
    examineDialog (dialogVisibleMod) {
      this.DialogVisibleExamine = false
    },
    //分页查询
    forData (page, size, content) {
      searchPageSize(page, size, content).then((response) => {
        // console.log(response);
        this.dataAttribute = response.data.list
        this.pagetotal = response.data.total
      })
    },
    //分页
    handleSizeChange (val) {
      console.log('每页 ' + val + ' 条')
      this.pagesize = val
      this.forData(this.currentPage, val, this.searchContent)
    },
    handleCurrentChange (val) {
      console.log('当前页: ' + val)
      // 序号
      // this.indexMethod()
      this.forData(val, this.pagesize, this.searchContent)
      this.currentPage = val
    },
    //查询组件回调
    searchCallBack (newData) {
      let obj = {}
      for (let i = 0; i < newData.length; i++) {
        obj[newData[i].EN] = newData[i].value
      }
      console.log(obj)
      this.searchContent = obj
    },
    //搜索
    sunS_F () {
      //console.log(this.searchContent); //用这组数据去ajax查询
      this.currentPage = 1
      this.forData(this.currentPage, this.pagesize, this.searchContent)
    },
    //增加
    toAdd (toAddData) {
      console.log('toAddData')
      console.log(toAddData, 358)
      // 原先的数组
      let abc = toAddData['abc']
      // 后加的form
      let def = toAddData['def']
      console.log(abc, def, 55)
      // debugger
      let obj = {}
      for (let i = 0; i < abc.length; i++) {
        obj[abc[i].EN] = abc[i].value
      }
      console.log(obj, def, '参数')
      obj['latitude'] = obj['latitude'] * 1
      obj['longitude'] = obj['longitude'] * 1
      // debugger
      let canshu = new FormData()
      canshu.append('name', obj.name)
      canshu.append('type', obj.type)
      canshu.append('phone', obj.phone)
      canshu.append('location', obj.location)
      canshu.append('latitude', obj.latitude)
      canshu.append('longitude', obj.longitude)
      canshu.append('notes', obj.notes)
      canshu.append('contract_tmpl', def.contractTmpl)
      canshu.append('open_AGRT', def.openAGRT)
      canshu.append('install_AGRT1', def.installAGRT1)
      canshu.append('install_AGRT2', def.installAGRT2)
      console.log(canshu, 999)
      postbaseTenantInformation(canshu)
        .then((response) => {
          // debugger
          if (response.flag) {
            this.$message({
              message: response.message,
              type: 'success',
            })
            this.forData(this.currentPage, this.pagesize, this.searchContent)
          } else {
            this.$message({
              message: response.message,
              type: 'warning',
            })
          }
        })
        .catch(() => {
          this.$message.error('添加失败')
        })
    },
    //批量删除
    toDelete (toDeleteData) {
      console.log(toDeleteData)
      console.log(this.multipleSelection)
      let arr = []
      for (let i = 0; i < this.multipleSelection.length; i++) {
        arr.push(this.multipleSelection[i].id)
        this.$confirm('确认批量删除？')
          .then((_) => {
            deleteBaseTenantInformation(arr).then((response) => {
              this.forData(this.currentPage, this.pagesize, this.searchContent) //刷新列表
            })
            done()
          })
          .catch((_) => { })
      }
    },
    //单项删除
    deleteList (id) {
      console.log(id)
      let arrId = [id]
      this.$confirm('确认删除？')
        .then((_) => {
          deleteBaseTenantInformation(arrId).then((response) => {
            this.$notify.info({
              title: response.message,
              message: '',
            })
            this.forData(this.currentPage, this.pagesize, this.searchContent) //刷新列表
          })
          done()
        })
        .catch((_) => {
          // this.$message({
          //   type:'warning',
          //   message:'已取消删除'
          // })
        })
    },
    //查看
    makeExaId (row) {
      let copyDialog = true
      this.DialogVisibleExamine = copyDialog
      this.examineData = row
    },
    //表格选中事件
    handleSelectionChange (val) {
      this.multipleSelection = val
      //传递删除的参数
      this.deleteDataList = val
    },
    //修改
    modifyData (value, MDData) {
      console.log(value, MDData)
      if (value) {
        console.log(id, MDData, '修改参数')
        // 原先的参数
        let abc = MDData['abc']
        let id = abc.id
        // 后加的form
        let def = MDData['def']
        let canshu = new FormData()
        canshu.append('name', abc.name)
        canshu.append('type', abc.type)
        canshu.append('phone', abc.phone)
        canshu.append('location', abc.location)
        canshu.append('latitude', abc.latitude)
        canshu.append('longitude', abc.longitude)
        canshu.append('notes', abc.notes)
        canshu.append('contract_tmpl', def.contractTmpl || abc.contractTmpl)
        canshu.append('open_AGRT', def.openAGRT || abc.openAgreement)
        canshu.append(
          'install_AGRT1',
          def.installAGRT1 || abc.installAgreement1
        )
        canshu.append(
          'install_AGRT2',
          def.installAGRT2 || abc.installAgreement2
        )
        console.log(canshu, 999)
        // debugger

        baseTenantInformation(id, canshu)
          .then((Response) => {
            if (Response.flag) {
              this.$message({
                message: Response.message,
                type: 'success',
              })
              this.forData(this.currentPage, this.pagesize, this.searchContent)
            } else {
              this.$message({
                message: Response.message,
                type: 'warning',
              })
            }
          })
          .catch(() => {
            this.$message.error(
              '错误信息：api/admin/baseTenantInformation/${id}！'
            )
          })
      }
    },
    // 字典组件回调
    dictionariesCallBack ([a = {}, b = {}]) {
      // 正常字典
      this.optionsZi = a
      // 跨表字典
      this.searchOptionTable = b
      console.log(a, b, 66688)
      // console.log(options, 111)
      // console.log(options,'huilaide');
      // this.optionsZi = options
      this.forData(this.currentPage, this.pagesize, this.searchContent)
      // }
    },
    // 序号
    indexMethod (index) {
      var currentPage = this.currentPage
      var pagesize = this.pagesize
      this.$nextTick(function () {
        currentPage = this.currentPage
        pagesize = this.pagesize
      })
      // console.log(this.pagesize,88888888888);
      return currentPage * pagesize - pagesize + 1 + index
      // return currentPage*pagesize - (pagesize-1)+index
      // currentPage*pagesize -pagesize +1+index
    },
  },
  computed: {
    // 字典计算属性
    optionDatas () {
      return function (arr, val) {
        //console.log(arr[val],'why');
        if (arr) {
          for (let i = 0; i < arr.length; i++) {
            if (arr[i].value == val) {
              // console.log(arr[i].label)
              return arr[i].label
            }
          }
        }
      }
    },
  },
}
</script>
<style scoped src="@/styles/allCssP.css"></style>
<style lang="scss" scoped>
.TenantInformationManagement {
  .shrinkBox {
    text-align: center;
    font-weight: 700;
    font-size: 20px;
    height: 25px;
    // border-bottom:1px solid #E6E6E6;
  }
  /deep/ .el-table--mini {
    .gutter {
      display: table-cell !important;
    }
    .el-checkbox__inner {
      width: 12px;
      height: 12px;
    }
    // 改变表头的高度  改为45px
    /deep/.el-table__header-wrapper {
      .el-table__header {
        .has-gutter {
          tr {
            th {
              padding: 5px 0 !important;
              height: 34px;
              line-height: 34px;
            }
            // .is-leaf{
            //   padding: 5px 0 !important;

            // }
          }
        }
      }
    }
    // 表头操作固定定位
    /deep/.el-table__fixed-right {
      .el-table__header {
        thead {
          tr {
            th {
              padding: 5px 0 !important;
              height: 34px;
              line-height: 34px;
              .cell {
                // padding-bottom: 5px !important;
                //   height: 34px;
                //   line-height: 34px;
              }
            }
          }
        }
      }
    }
  }
  .right_row {
    display: flex;
    // justify-content: start;
    align-items: center;
    position: relative;

    .buttons {
      position: absolute;
      padding: 0px !important;
      right: 30px;
    }
    .buttonhurdle {
      padding: 0px !important;
    }
  }
  .do {
    display: flex;
  }
  .pagination {
    text-align: left;
    margin-top: 20px;
  }
}
</style>
